package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilerChainService;
import com.qf.util.CheckMobileOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * projectName: sms-platform02
 *
 * @author: 元
 * time: 2021/10/18 0018 16:36
 * description:号段补全策略实现类
 * 根据发生目标手机号,识别运营商和归属地
 */
@Service(value = "phase")
@Slf4j
public class PhaseServiceImpl implements FilerChainService {

    @Autowired
    private CacheFeign cacheFeign;



    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块-号段补全],开始执行,standardSubmit={}",standardSubmit);

        //1.获取发送目标手机号
        String destMobile = standardSubmit.getDestMobile();
        //2.根据目标手机号判断对应运营商
        if (CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
            //判断是否是中国移动
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);     //运营商ID
            standardSubmit.setGatewayID(OperatorConstants.CHINA_MOBILE);      //下发网关ID,即通道ID
        }else if (CheckMobileOperator.isChinaUnicomPhoneNum(destMobile)){
            //判断是否是中国联通
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);     //运营商ID
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNICOM);      //下发网关ID,即通道ID
        }else if (CheckMobileOperator.isChinaTelecomPhoneNum(destMobile)){
            //判断是否是中国电信
            standardSubmit.setOperatorId(OperatorConstants.CHINA_TELECOM);     //运营商ID
            standardSubmit.setGatewayID(OperatorConstants.CHINA_TELECOM);      //下发网关ID,即通道ID
        }else {
            //未知号码
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNKNOWN);      //下发网关ID,即通道ID
        }

        //3.根据目标手机号和redis中获取的号段数据判断号码归属地(省市区)
            //将目标手机号前7位截取出来,作为 号段判断的标准
        String key = destMobile.substring(0,7);
            //从redis中获取号段数据
        String value = cacheFeign.get(CacheConstants.CACHE_PREFIX_PHASE + key);
        if (StringUtils.isEmpty(value)){
            log.info("[策略模块-号段补全],目标手机号有误,查询不到归属地,目标手机号={}",standardSubmit,destMobile);
            return false;
        }
            //从redis中获取号码归属地
        String [] provinceAndCity = value.split(",");
        if (provinceAndCity.length != 2){
            log.info("[策略模块-号段补全],目标手机号有误,查询不到归属地,目标手机号={}",standardSubmit,destMobile);
            return false;
        }
        standardSubmit.setProvinceId(Integer.parseInt(provinceAndCity[0]));
        standardSubmit.setCityId(Integer.parseInt(provinceAndCity[1]));

        //4.根据运营商ID和网关ID从redis中获取路由通道的数据
        Map<String,Object> routerMap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_ROUTER + standardSubmit.getClientID() + ":" + standardSubmit.getGatewayID());

        //5.获取redis中的通道数据
        Map<String,Object> channelMap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_SMS_CHANNEL_ALL + standardSubmit.getOperatorId() + ":" + standardSubmit.getGatewayID());

        //6.根据路由数据和通道数据,以及目标手机号,匹配出短信的下发源号码,以及当前短信的费用
        if (routerMap != null && channelMap != null){
            //获取账户接入号
            String spNumber = String.valueOf(channelMap.get("spnumber"));
            //获取扩展号
            String exendNumber = String.valueOf(routerMap.get("exendnumber"));
            //下发源号码 = 接入号+扩展号
            String srcNumber = spNumber + exendNumber;
            //再计算当前短信费用
            standardSubmit.setFee(Long.parseLong(String.valueOf(routerMap.get("price"))));
        }
        log.info("[策略模块-号段补全],号段补全完成,standardSubmit={}",standardSubmit,destMobile);
        return true;
    }
}
